const Article = require('../models/Article');
const ErrorResponse = require('../util/errorResponse');
const BaseController = require('./base');

const includeOptions = [];

class ArticleController extends BaseController {
  static async getArticles(ctx) {
    const { limit = 20, offset = 0 } = ctx.request.query;
    // const { loggedUser } = req;

    const searchOptions = {
      include: [],
      limit: parseInt(limit),
      offset: parseInt(offset),
      order: [['created_at', 'DESC']],
      distinct: true
    };

    let articles = { rows: [], count: 0 };

    articles = await Article.findAndCountAll(searchOptions);
    ctx.body = BaseController.renderJsonSuccess(200, '', {
      articles: articles.rows,
      articlesCount: articles.count
    });
  }
  static async getArticle(ctx) {
    const { id } = ctx.params;

    const article = await Article.findOne({
      where: { id: id },
      include: includeOptions
    });

    if (!article) return next(new ErrorResponse('Article not found', 404));
    ctx.body = BaseController.renderJsonSuccess(200, '', {
      article: article
    });
  }
  static async deleteArticle(ctx) {
    const { id } = ctx.params;

    const res = await Article.destroy({
      where: { id: id }
    });

    ctx.body = BaseController.renderJsonSuccess(200, '', { res: res });
  }
  static async createArticle(ctx) {
    const data = ctx.request.body;

    const newArticle = await Article.create({
      title: data.title,
      description: data.description,
      content: data.content,
      userid: 0
    });

    if (!newArticle) return next(new ErrorResponse('create fail', 404));
    ctx.body = BaseController.renderJsonSuccess(200, '', {
      article: newArticle
    });
  }
  static async updateArticle(ctx) {
    const { id } = ctx.params;
    const data = ctx.request.body;

    const res = await Article.update(
      {
        title: data.title,
        description: data.description,
        content: data.content
      },
      {
        where: { id: id }
      }
    );

    ctx.body = BaseController.renderJsonSuccess(200, '', { res: res });
  }
}
module.exports = ArticleController;

// module.exports.deleteArticle = async (req, res, next) => {
//   const { id } = req.params;

//   const article = await Article.findOne({
//     where: { id: id },
//     include: includeOptions
//   });

//   if (!article) next(new ErrorResponse('Article not found', 404));

//   // if (article.authorId !== loggedUser.id)
//   //   return next(new ErrorResponse('Unauthorized', 401));

//   await article.destroy();

//   res.status(200).json({ article });
// };

// module.exports.updateArticle = async (req, res, next) => {
//   const { id } = req.params;
//   // const { loggedUser } = req;

//   const article = await Article.findOne({
//     where: { id: id },
//     include: includeOptions
//   });

//   if (!article) next(new ErrorResponse('Article not found', 404));

//   // if (article.authorId !== loggedUser.id)
//   //   return next(new ErrorResponse('Unauthorized', 401));

//   const { title, description, content } = req.body.article;

//   await article.update({
//     title: title ? title : article.title,
//     description: description ? description : article.description,
//     content: content ? content : article.content
//   });

//   res.status(200).json({ article });
// };

// module.exports.getArticle = async (req, res, next) => {
//   // const { loggedUser } = req;
//   const { id } = req.params;

//   const article = await Article.findOne({
//     where: { id: id },
//     include: includeOptions
//   });

//   if (!article) return next(new ErrorResponse('Article not found', 404));

//   res.status(200).json({ article });
// };

// const fieldValidation = (field, next) => {
//   if (!field) {
//     return next(new ErrorResponse(`Missing fields`, 400));
//   }
// };
